<?php
namespace classlib\core\view;
use classlib\core\router;
class js extends \classlib\core\view\html {	
    /**
     * 程序解析
     * @param 
     * (non-PHPdoc)
     * @see view_abstract::display()
     */
    public function display($tpl=false, $cacheid=null, $lifetime = -1) {
        if ($this->caching && $this->cache) {
        	$szkey = 'view@'.md5($cacheid.$tpl);
        	$this->cache->set($szkey, $this->data);
        }
        $html = $this->fetch($tpl);
        $this->out($html, false);
    }
    
    /**
     * 直接将一个字符串输出缓冲区
     * @param unknown $html
     */
    public function out($html, $is_ob_clean=true) {
    	header('Content-Type: text/javascript;charset=utf-8');
    	$html = 'document.write(\''.str_replace(array('\'', "\r\n", "\n"), array('\\\'', '', ''), $html).'\');';
    	if ($this->publish_dir) {
    		if (empty(router::$uri)) {
    			router::$uri  = 'index';
    		}
    		router::$uri .= '.js';
    		$staticfile   = $this->publish_dir.router::$uri;
    		$this->mkdirs(dirname($staticfile));
    		@file_put_contents($staticfile, $html, LOCK_EX) or die('html_view publish dir write access deny');
    	}
    	$is_ob_clean && ob_end_clean();
    	echo $html;
    }
}